var a = getApp();

function get(url, data = {}) {
  var parmes = {
    openid: wx.getStorageSync('openid')
  };
  return new Promise(function (o, r) {
    wx.request({
      url: a.globalData.baseUrl + url,
      data: {
        ...data,
        ...parmes
      },
      method: "GET",
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
      },
      success: function (e) {
        o(e.data);
        if (e.data.code == -1) {
          wx.switchTab({
            url: '/pages/index/index',
          })
        } else if (e.data.code == 0) {
          const err = e.data.msg ? e.data.msg : '出错误了';
          wx.showToast({
            title: err,
            icon: 'none'
          })
        }
      },
      fail: function (e) {
        wx.showToast({
          title: '网络连接异常',
          icon: 'none'
        })
      },
      complete: function () {
        wx.hideLoading()
      }
    });
  });
}

function post(url, data = {}) {
  var parmes = {
    openid: wx.getStorageSync('openid')
  };
  return new Promise(function (o, r) {
    wx.request({
      url: a.globalData.baseUrl + url,
      data: {
        ...data,
        ...parmes
      },
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
      },
      success: function (e) {
        o(e.data);
        if (e.data.code == -1) {
          o(e.data);
          wx.switchTab({
            url: '/pages/index/index',
          })
        } else if (e.data.code == 0) {
          const err = e.data.msg ? e.data.msg : '出错误了';
          wx.showToast({
            title: err,
            icon: 'none'
          })
        }
      },
      fail: function (e) {
        wx.showToast({
          title: '网络连接异常',
          icon: 'none'
        })
      },
      complete: function () {
        wx.hideLoading()
      }
    });
  });
}

var u = {
  toLogin: function (obj = null) {
    return new Promise(function (reject) {
      wx.login({
        success: function (o) {
          let parms = {
            code: o.code
          }
          if (obj && obj.matchmaker_id) parms.matchmaker_id = obj.matchmaker_id;
          post('wechat/Apiwechat/getOpenid', parms).then(res => {
            wx.setStorageSync('session_key', res.data.session_key)
            wx.setStorageSync('openid', res.data.openid)
            if (res.data.unionid && res.data.unionid != undefined) {
              wx.setStorageSync('unionid', res.data.unionid)
            }
            reject(true)
          })
        },
        fail: function (t) {
          wx.showToast({
            title: '网络连接异常',
            icon: 'none'
          })
        }
      });
    });
  },
  // 保存微信昵称和头像
  saveWechat(data) {
    wx.showLoading();
    return post("wechat/Basejson/saveWechat", data);
  },
  // 上传头像
  setMemberInfo(data) {
    wx.showLoading();
    return post("wechat/home/setMemberInfo", data);
  },
  // 获取微信电话
  getWxTel(data) {
    return post("wechat/Apiwechat/wxtel", data);
  },
  // 获取会员详情
  getMemberDetail(data) {
    return post("wechat/home/getMemberDetail", data);
  },
  // 申请预约
  orderList(data) {
    return post("wechat/Member/orderList", data);
  },

   // 索赔列表
   myInclaimantList(data) {
    return post("wechat/home/getmyInclaimantList", data);
  },
     // 进度列表
  myInclaimantProcess(data) {
        return post("wechat/home/getmyInclaimantProcess", data);
  },
    // 索赔详情
  _serverInclaimantDetail(data) {
        return get("wechat/Home/serverInclaimantDetail", data);
  },
  // 股票列表
  serveList(data) {
    return post("wechat/Home/getServerList", data);
  },
  // 股票详情
  serveDetail(data) {
    return get("wechat/Home/getServerDetail", data);
  },
  config() {
    return get("wechat/Apiwechat/share_config");
  },
  getUserInfo() {
    return post("wechat/Member/getUserInfo");
  },
 
  //添加索赔
  addPproceeding(data){
    return post("wechat/Home/serverInclaimant", data);
  }
};

exports.default = u;